package czy.demo.controller;

import czy.demo.pojo.User;
import czy.demo.view.PDFView;
import czy.demo.view.XLSView;
import czy.demo.view.XLSXView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
import org.springframework.web.servlet.view.xml.MappingJackson2XmlView;

import java.util.Arrays;
import java.util.List;


@Controller
@RequestMapping("/view")
public class ViewController {

    public static final Logger logger = LoggerFactory.getLogger(ViewController.class);

    /* json视图 */
    @GetMapping("/json")
    public ModelAndView getJSON(){
        /* 视图只是输出方式，数据都在Model里 */
        MappingJackson2JsonView view = new MappingJackson2JsonView();
        ModelMap model = new ModelMap();
        model.put("id",1);
        model.put("name","张三");
        model.put("age",23);
        return new ModelAndView(view,model);
    }

    /* xml视图 */
    @GetMapping("/xml")
    public ModelAndView getXml(){
        MappingJackson2XmlView view = new MappingJackson2XmlView();
        /* 需要设置一个modelKey，这个key对应的value会被渲染为xml，如果不设置，会报错 */
        view.setModelKey("user");
        ModelMap model = new ModelMap();
        model.put("user",new User("张三",23));
        return new ModelAndView(view,model);
    }

    /* pdf视图 */
    @GetMapping("/pdf")
    public ModelAndView getPdf(){
        PDFView view = new PDFView();
        ModelMap model = new ModelMap();
        List<User> users = Arrays.asList(new User("张三",23),new User("李四",34),new User("王五",67));
        model.put("users",users);
        return new ModelAndView(view,model);
    }

    /* xls视图 */
    @GetMapping("/xls")
    public ModelAndView getXls(){
        XLSView view = new XLSView();
        ModelMap model = new ModelMap();
        List<User> users = Arrays.asList(new User("张三",23),new User("李四",34),new User("王五",67));
        model.put("users",users);
        return new ModelAndView(view,model);
    }

    /* xlsx视图 */
    @GetMapping("/xlsx")
    public ModelAndView getXlsx(){
        XLSXView view = new XLSXView();
        ModelMap model = new ModelMap();
        List<User> users = Arrays.asList(new User("张三",23),new User("李四",34),new User("王五",67));
        model.put("users",users);
        return new ModelAndView(view,model);
    }

    /* xslt视图 */
    @GetMapping("/xslt")
    public ModelAndView getXslt(){
        return new ModelAndView();
    }


}
